<template>
  <div>
    <el-row>
      <div>
        <el-col :span="3"> <p>原料名称</p></el-col>
        <el-col :span="5">
          <el-input v-model="keyword" placeholder="请输入查询材料"></el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" icon="el-icon-search" @click="searchData()"
            >查询</el-button
          >
        </el-col>
        <el-col :span="3">
          <el-button type="success" icon="el-icon-search" @click="reload()"
            >清空</el-button
          >
        </el-col>
      </div>
    </el-row>
    <div style="width: 80%">
      <div id="bar" style="width:500px; height: 400px"></div>
    </div>
    <div style="width: 80%">
      <div id="pie" style="width: 100%; height: 400px"></div>
    </div>
  </div>
</template>
<script>
import * as echarts from "echarts";

export default {
  data() {
    return {
    keyword:"",
    };
  },
  methods: {
   searchData(){
    this.$axios.post('/data/searchFproduct/'+this.keyword)
    .then(res=>{
      console.log(res.data.data)
        const data = res.data.data;
          const proName = data.map((item) => item.saleprice);
          const proNum = data.map((item) => item.stock);
          const chartData = data.map((item) => ({
            name: item.saleprice,
            value: item.stock,
          }));
          this.initBar(proName, proNum);
          this.initpie(chartData);
      }
    )
   },
   initBar(proName, proNum) {
      let chartDom = document.getElementById("bar");
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: this.keyword+"库存表",
          left: "center",
        },
        xAxis: {
          type: "category",
          data: proName,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            data: proNum,
            type: "bar",
             itemStyle: {
            color: '#73c0de'
          }
          },
        ],
      };

      option && myChart.setOption(option);
    },
    initpie(chartData) {
      let chartDom = document.getElementById("pie");
      let myChart = echarts.init(chartDom);
      let option;

      option = {
        title: {
          text: this.keyword+"库存表",
          
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
           
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: "50%",
            data:chartData ,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },reload(){
      this.keyword=''
    }
  },
};
</script>
<style scoped>
</style>